package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsPurchaseOrderDetailed;
import com.jy.wms.service.WmsPurchaseOrderDetailedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: WmsPurchaseOrderDetailedController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author lh
 * @Date Fri Sep 15 17:40:52 GMT+08:00 2017
 */

@RestController
@RequestMapping("/wmsPurchaseOrderDetailed")
@Api(value = "/wmsPurchaseOrderDetailed", description = "关于采购订单明细操作")
public class  WmsPurchaseOrderDetailedController extends BaseController<WmsPurchaseOrderDetailedService,WmsPurchaseOrderDetailed> {


    @ApiOperation(value = "订单添加", response = JSONObject.class, notes = "添加采购订单")
    @PostMapping(value = "add")
    public @ResponseBody
    JSONObject addWmsPurchaseOrderDetailed(@RequestBody WmsPurchaseOrderDetailed entity, HttpSession session) {
        entity.setId(UUIDUtils.uuid());
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        entity.setCkid(ckid);
        try {
                baseService.baseInsert(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "采购订单增加成功", "", JSONObject.fromObject(entity).toString());

        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "采购订单增加成功", e.getMessage(), JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            Boolean a = baseService.deleteById(entity.getId());
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "采购订单增加失败", e.getMessage(), "");
        } catch (Exception e) {
            Boolean b = baseService.deleteById(entity.getId());
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "采购订单增加失败", e.getMessage(), "");
        }
        // return renderSuccess("添加成功");
    }


    @ApiOperation(value = "采购订单明细编辑", response = JSONObject.class, notes = "编辑采购订单明细")
    @PostMapping(value = "edit")
    public @ResponseBody
    JSONObject edit(@RequestBody WmsPurchaseOrderDetailed entity, HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        try {
            baseService.baseUpdateMx(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "采购订单明细修改成功", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "采购订单明细修改成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "采购订单明细修改失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "采购订单明细修改失败", e.getMessage(), "");
        }
    }


    @ApiOperation(value = "采购订单明细删除", response = JSONObject.class, notes = "查询输入</br>{\"ids\":\"明细id集合\",\"id\":\"订单id\"}")
    @PostMapping(value = "delete")
    public @ResponseBody
    JSONObject delete(@RequestBody SetAndModifyModel setModel, HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        try {
            baseService.baseDeleteByPrimaryKey(setModel.getIds(),setModel.getId());
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "采购订单明细删除成功", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "采购订单明细删除成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "采购订单明细删除失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "采购订单明细删除失败", e.getMessage(), "");
        }
    }





}
